package dp

func MaxArea(height []int) int {
	// 暴力解法：两层循环遍历
	maxVolume := 0
	/*for i := 0; i < len(height) - 1; i++ {
		for j := i + 1; j < len(height); j++ {
			currentHeight := height[i]
			if height[j] < currentHeight {
				currentHeight = height[j]
			}
			currentVolume := (j - i) * currentHeight
			if maxVolume < currentVolume {
				maxVolume = currentVolume
			}
		}
	}*/

	// 双指针解法
	startIndex := 0
	endIndex := len(height) - 1
	for startIndex < endIndex {
		currentHeight := height[startIndex]
		if currentHeight > height[endIndex] {
			currentHeight = height[endIndex]
		}
		currentVolume := currentHeight * (endIndex - startIndex)
		if currentVolume > maxVolume {
			maxVolume = currentVolume
		}

		if height[startIndex] > height[endIndex] {
			endIndex--
		} else {
			startIndex++
		}
	}

	return maxVolume
}
